<template>
  <div id="examinpaper_add_edit">
    <el-form label-width="140px">
      <el-form-item label="标题" :rules="[
      { required: true, message: ''}]">
        <el-input
          style="width:60%"
          ref="title"
          clearable
          v-model="examinpaperData.title"
          placeholder="请输入评测试卷标题"
        ></el-input>
      </el-form-item>
      <el-form-item label="适用年龄" :rules="[
      { required: true, message: ''}]">
        <div>
          <el-select
            ref="ageStart"
            v-model="examinpaperData.ageStart"
            @change="ageStartChange(2)"
            clearable
            style="width: 1.76rem;"
            placeholder="开始年龄"
          >
            <el-option
              v-for="item in ageStartList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
              v-show="item.show"
            ></el-option>
          </el-select>&nbsp;~
          <el-select
            ref="ageEnd"
            v-model="examinpaperData.ageEnd"
            @change="ageEndChange(2)"
            clearable
            style="width: 1.8rem;"
            placeholder="结束年龄"
          >
            <el-option
              v-for="item in ageEndList"
              :key="item.value"
              :label="item.name"
              :value="item.value"
              v-show="item.show"
            ></el-option>
          </el-select>&nbsp;岁
        </div>
      </el-form-item>
    </el-form>
    <div class="btn_div">
      <div class="btn_sure" @click="sure()">确 定</div>
      <div class="btn_cancel" @click="cancel()">取 消</div>
    </div>
  </div>
</template>

<script>
var vm, user;
export default {
  data: function() {
    return {
      isAdd: true,
      ageStartList: [],
      ageEndList: [],
      examinpaperData: {}
    };
  },
  methods: {
    init: function(data) {
      if (data && data.paperId != null) {
        vm.isAdd = false;
        vm.examinpaperData = data;
      } else {
        vm.isAdd = true;
        vm.examinpaperData = {
          title: "",
          evaluateType: 1,
          level: 1,
          ageStart: "",
          ageEnd: "",
          limitTime: 60,
          organizationId: user.orgId
        };
      }
    },
    cancel: function() {
      vm.examinpaperData = {};
      vm.$emit("cancel");
    },
    ageStartChange: function() {
      //开始年龄发生改变
      //限制结束年龄
      vm.ageEndList.forEach(function(item) {
        if (
          !vm.examinpaperData.ageStart ||
          parseInt(item.value) >= parseInt(vm.examinpaperData.ageStart)
        ) {
          item.show = true;
        } else {
          item.show = false;
        }
      });
    },
    ageEndChange: function() {
      //结束年龄发生改变
      //限制开始年龄
      vm.ageStartList.forEach(function(item) {
        if (
          !vm.examinpaperData.ageEnd ||
          parseInt(item.value) <= parseInt(vm.examinpaperData.ageEnd)
        ) {
          item.show = true;
        } else {
          item.show = false;
        }
      });
    },
    sure: function() {
      var rules = [];
      let title = this.$refs.title;
      let ageStart = this.$refs.ageStart;
      let ageEnd = this.$refs.ageEnd;
      console.log(vm.examinpaperData.title);
      rules.push({
        judge: tool.judge("courseName", vm.examinpaperData.title),
        focus: title,
        tips: "请输入请输入1-50位由字母汉字数字（）_-组成的评测试卷标题"
      });
      // rules.push({
      //   judge: vm.examinpaperData.title.indexOf("初级") != -1 ||  vm.examinpaperData.title.indexOf("中级") != -1 || vm.examinpaperData.title.indexOf("高级") != -1,
      //   focus: title,
      //   tips: "请输入包含“初级”/“中级”/“高级”关键字的评测试卷标题"
      // });
      vm.examinpaperData.ageStart = ""+vm.examinpaperData.ageStart;
      vm.examinpaperData.ageEnd = ""+vm.examinpaperData.ageEnd;
      rules.push({
        judge:  vm.examinpaperData.ageStart,
        focus: ageStart,
        tips: "请选择开始年龄"
      });
      rules.push({
        judge: vm.examinpaperData.ageEnd,
        focus: ageEnd,
        tips: "请选择结束年龄"
      });
      if (!tool.validate(rules)) {
        return;
      }
      if (vm.isAdd) {
        api.examinpaperAdd(vm.examinpaperData).then(function(data) {
          if (data.resultCode == 2000) {
            vm.$message({
              message: "添加成功",
              type: "success"
            });
            vm.$emit("ok", "添加成功");
            vm.cancel();
          } else {
            vm.$message.error(data.message);
          }
        });
      } else {
         api.examinpaperUpdate([vm.examinpaperData]).then(function(data) {
          if (data.resultCode == 2000) {
            vm.$message({
              message: "编辑成功",
              type: "success"
            });
            vm.$emit("ok", "添加成功");
            vm.cancel();
          } else {
            vm.$message.error(data.message);
          }
        });
      }
    }
  },
  mounted() {
    vm = this;
    user = tool.getItem(lsKey.user, true); //得到当前用户信息
    for (let i = 0; i < 100; i++) {
      vm.ageStartList.push({ name: "" + i, value: "" + i, show: true });
      vm.ageEndList.push({ name: "" + i, value: "" + i, show: true });
    }
  }
};
</script>
<style lang="scss">
#examinpaper_add_edit {
  .el-textarea__inner {
    font-size: 0.16rem;
    font-family: "微软雅黑";
  }
}
</style>
<style scoped src="@/style/module/dialog_button.css"></style>
